<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MlcmsUsertransfer extends Model
{
    protected $table="mlmcms_user_transferlog";
    public $timestamps=false;
    protected $fillable = [];
    protected $guarded =[];
    protected $primaryKey = "MUTID";
    /**
     * 
     * @param type $request
     * @param type $type
     */
    public static function AddTransferLog(\Illuminate\Http\Request $request, $type) {
        if ($type == 2) {
            return static::validateJToD($request);
        } elseif ($type == 1) {
            return static::validateDZB($request);
        }
    }
    
    /**
     * 验证金币转电子币
     * @param type $request
     */
    public static function validateJToD(\Illuminate\Http\Request $request) {
        $user = $request->session()->get("Home");
        if($request->input("Mut_OutMoney") < 1 || $request->input('Mut_OutMoney') > $user->Mu_AllMoney) {
            return aaa_return(0, "操作失败,转换数目不合法");
        }
        //转换
        $data1['Mu_AllMoney'] = $user->Mu_AllMoney - $request->input("Mut_OutMoney");
        $data1['Mu_BuyFunds'] = $user->Mu_BuyFunds + $request->input("Mut_OutMoney");
        \Illuminate\Support\Facades\DB::beginTransaction();
        if (!MlcmsUser::where("MUID", $user->MUID)->update($data1)) {
            \Illuminate\Support\Facades\DB::rollBack();
            return aaa_return(0, "转换失败");
        }
        //添加纪录
        $data['Mut_Number'] = $user->Mu_Number;
        $data['Mut_OutMoney'] = $request->input('Mut_OutMoney');
        $data['Mut_AddMoney'] = $request->input('Mut_OutMoney');
        $data['Mut_RecNumber'] = $user->Mu_Number;
        $data['Mut_Type'] = 2;
        $data['Mut_CreateTime'] = date("Y-m-d H:i:s");
        $data['Mut_OldMoney'] = $user->Mu_AllMoney;
        $data['Mut_NewMoney'] = $data1['Mu_AllMoney'];
        $data['Mut_RecOldMoney'] = $user->Mu_BuyFunds;
        $data['Mut_RecNewMoney'] = $data1['Mu_BuyFunds'];
        if(!static::insert($data)) {
            \Illuminate\Support\Facades\DB::rollBack();
            return aaa_return(0, "转账失败");
        }
        $user->Mu_AllMoney = $data1['Mu_AllMoney'];
        $user->Mu_BuyFunds = $data1['Mu_BuyFunds'];
        \Illuminate\Support\Facades\DB::commit();
        return aaa_return(1, "转换成功");
    }
     /**
     * 电子币互转
     * @param type $request
     */
    public static function validateDZB(\Illuminate\Http\Request $request) {
        $user = $request->session()->get("Home");
        $recuser = MlcmsUser::where("Mu_Number", $request->input("Mut_RecNumber"))->first();
        if (!$recuser) {
            return aaa_return(0, "接收人不存在,请确认后再转");
        }
        if($request->input("Mut_OutMoney") < 1 || $request->input('Mut_OutMoney') > $user->Mu_BuyFunds) {
            return aaa_return(0, "操作失败,转换数目不合法");
        }
        //转换
        $data1['Mu_BuyFunds'] = $user->Mu_BuyFunds - $request->input("Mut_OutMoney");
        $data2['Mu_BuyFunds'] = $recuser->Mu_BuyFunds + $request->input("Mut_OutMoney");
        \Illuminate\Support\Facades\DB::beginTransaction();
        if (!MlcmsUser::where("MUID", $user->MUID)->update($data1) || !MlcmsUser::where("MUID", $recuser->MUID)->update($data2)) {
            \Illuminate\Support\Facades\DB::rollBack();
            return aaa_return(0, "转换失败");
        }
        //添加纪录
        $data['Mut_Number'] = $user->Mu_Number;
        $data['Mut_OutMoney'] = $request->input('Mut_OutMoney');
        $data['Mut_AddMoney'] = $request->input('Mut_OutMoney');
        $data['Mut_RecNumber'] = $recuser->Mu_Number;
        $data['Mut_Type'] = 1;
        $data['Mut_CreateTime'] = date("Y-m-d H:i:s");
        $data['Mut_OldMoney'] = $user->Mu_BuyFunds;
        $data['Mut_NewMoney'] = $data1['Mu_BuyFunds'];
        $data['Mut_RecOldMoney'] = $recuser->Mu_BuyFunds;
        $data['Mut_RecNewMoney'] = $data2['Mu_BuyFunds'];
        if(!static::insert($data)) {
            \Illuminate\Support\Facades\DB::rollBack();
            return aaa_return(0, "转账失败");
        }
        $user = MlcmsUser::where('MUID', $user->MUID)->first();
        \Illuminate\Support\Facades\DB::commit();
        return aaa_return(1, "转帐成功");
    }
    
    /**
     * 获取类型
     */
    public function getType() {
        switch ($this->Mut_Type) {
            case 1:
                return "电子币互转";
            case 2:
                return "奖金币转电子币";
            case 3:
                return "";
            case 4:
                return "";
            default :
                return "未知";
        }
    }
}
